package com.ninong.ker.admin.rest;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ninong.ker.admin.domain.Menu;
import com.ninong.ker.admin.service.MenuService;
import com.ninong.ker.common.annotation.log.Log;
import com.ninong.ker.common.annotation.perm.CmdAdmin;
import com.ninong.ker.common.response.CmdResponse;
import com.ninong.ker.common.tools.enums.BusinessEnum;

/**
 * 菜单资源
 * 
 * @author cmd
 *
 */
@RestController
@RequestMapping("/cmd/v1/menu")
public class MenuResouces {

	@Autowired
	private MenuService menuService;

	@Log("菜单列表")
	@CmdAdmin({ "admin", "menu:list" })
	@PostMapping
	public ResponseEntity<CmdResponse> list(@RequestBody Menu dto) {
		CmdResponse response = new CmdResponse();
		response.setData(menuService.listTree(dto));
		return ResponseEntity.ok(response);
	}

	@Log("菜单删除")
	@CmdAdmin({ "admin", "menu:delete" })
	@DeleteMapping
	public ResponseEntity<CmdResponse> delete(@RequestBody String[] ids) {
		CmdResponse response = new CmdResponse();
		menuService.delete(ids);
		response.setMessage(BusinessEnum.DELETE_SUCCESS.getMsg());
		return ResponseEntity.ok(response);
	}

	@Log("菜单新增或编辑")
	@CmdAdmin({ "admin", "menu:add", "menu:update" })
	@PutMapping
	public ResponseEntity<CmdResponse> create(@RequestBody Menu entity) {
		menuService.create(entity);
		return ResponseEntity.ok(new CmdResponse());
	}

	@Log("菜单详情")
	@GetMapping(value = "/{id}")
	public ResponseEntity<CmdResponse> get(@PathVariable String id) {
		CmdResponse response = new CmdResponse();
		response.setData(menuService.get(id));
		return ResponseEntity.ok(response);
	}

	@Log("获取已选中数据")
	@GetMapping(value = "/tree/{roleId}")
	public ResponseEntity<CmdResponse> list(@PathVariable String roleId) {
		CmdResponse response = new CmdResponse();
		response.setData(menuService.listTreeByRoleId(roleId));
		return ResponseEntity.ok(response);
	}

	@Log("用色菜单关系更新")
	@PostMapping(value = "/merge/{roleId}")
	public ResponseEntity<CmdResponse> updateRoleMenuByRoleId(@PathVariable String roleId, @RequestBody List<String> menuIds) {
		menuService.updateRoleMenuByRoleId(roleId, menuIds);
		return ResponseEntity.ok(new CmdResponse());
	}

}
